#coding=utf-8

import commands
# import tools.excelOperate
from tools.excelOperate import *
def linux_op():
    status, output = commands.getstatusoutput('ls -l')
    print output.find('test.sh')
    startStr = '28 Apr '
    startline = output.find(startStr) + len(startStr)
    endline = output.find('test.sh')
    print 'start = ' + str(startline) + '; endline = ' + str(endline)
    print output[startline:endline]

Concurrent_number =5
iterations = 20

class Stress_Test_Class():

    def Average_response_time(self,command):
        # status, output = commands.getstatusoutput(command)
        # startStr = 'Time per request:       '
        # startline = output.find(startStr) + len(startStr)
        # endline = output.find(' [ms] (mean)')
        # Response_time = output[startline:endline]
        Response_time = 10
        print  'Response_time = '+ str(Response_time)
        return Response_time

    def Stress_Test(self):
        title_list = ['Concurrent_number','iterations','timer']
        response_time_instance = Stress_Test_Class()
        excel_op_instance = tools.excelOperate.excel_Operate()
        file_name  = excel_op_instance.create_excel()
        print 'filename = '+ file_name
        excel_op_instance.excel_context_op(file_name,0,title_list)
        rowIndex = 1
        for Concurrent_number in range(5,21,5):
            for iterations in range(20,101,20):
                print 'Concurrent_number = '+ str(Concurrent_number) + ';iterations = '+ str(iterations)
                testUrl = ' https://www.baidu.com/'
                command = 'ab -c '+str(Concurrent_number) + ' -n '+ str(iterations) + testUrl
                print  'command = '+command
                Response_time =  response_time_instance.Average_response_time(command)
                resulut_lise = [Concurrent_number,iterations,Response_time]
                # print 'excel write function---'
                excel_op_instance.excel_context_op(file_name,rowIndex,resulut_lise)
                rowIndex = rowIndex + 1

if __name__ == '__main__':
    test = Stress_Test_Class()
    test.Stress_Test()
